package com.example.occlusiondemo.view;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.SeekBar;

import com.example.occlusiondemo.Status;
import com.supermap.ar.areffect.AREffectView;
import com.supermap.ar.areffect.EffectView;

/**
 *
 * <p>管理开挖参数调节的手势</p>
 * <pre>SampleCode:
 * </pre>
 *
 * @author tanyx 2022/9/28
 * @version 1.0
 **/
public class DigGestureManager {
    public interface DigGestureCallback{
        boolean onDoubleTap(MotionEvent e);
        void onShowPress(MotionEvent e);
    }

    public static DigGestureManager instance = null;
    private final float SCALE_FACTOR = 0.2f;

    private boolean bind = false;

    private GestureDetector gestureDetector;

    private CustomGesture modifiedGesture;

    private CustomGesture.Callback callback2;
    private CustomGesture.Callback callback3;
    private DigGestureCallback digGestureCallback;

    public void setDigGestureCallback(DigGestureCallback digGestureCallback) {
        this.digGestureCallback = digGestureCallback;
    }

    private DigGestureManager() {
        modifiedGesture = new CustomGesture();
    }

    public void bind(EffectView effectView, SeekBar dig2RadiusSeekBar, SeekBar dig2DepthSeekBar, SeekBar dig3LengthSeekBar,
                     SeekBar dig3WidthSeekBar, SeekBar dig3DepthSeekBar){
        if (!bind){
            gestureDetector = new GestureDetector(effectView.getContext(),modifiedGesture);
            //禁用长按，因为长按后无法进入onScroll
            gestureDetector.setIsLongpressEnabled(false);

            int[] callbackOriginValue = new int[3];
            int[] callbackMaxValue = new int[3];
            callback2 = new CustomGesture.Callback() {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    if (digGestureCallback != null){
                        return digGestureCallback.onDoubleTap(e);
                    }
                    return false;
                }

                @Override
                public void onShowPress(MotionEvent e) {
                    if (digGestureCallback != null){
                        digGestureCallback.onShowPress(e);
                    }
                }

                @Override
                public void onDown(MotionEvent e) {
                    callbackOriginValue[0] = dig2RadiusSeekBar.getProgress();
                    callbackOriginValue[1] = dig2DepthSeekBar.getProgress();
                    callbackMaxValue[0] = dig2RadiusSeekBar.getMax();
                    callbackMaxValue[1] = dig2DepthSeekBar.getMax();
                }

                @Override
                public void onScaleX(float scaleFactor) {
                    if (Status.editedParameter){
                        dig2RadiusSeekBar.setProgress(Math.max(0,Math.min(callbackMaxValue[0],/*计算*/(int) (scaleFactor * callbackOriginValue[0]))));
                    }
                }

                @Override
                public void onScaleY(float scaleFactor) {
                    if (Status.editedParameter){
                        dig2RadiusSeekBar.setProgress(Math.max(0,Math.min(callbackMaxValue[0],/*计算*/(int) (scaleFactor * callbackOriginValue[0]))));
                    }
                }

                @Override
                public void onTranslation(float x, float y) {
                    if (Status.editedParameter){
                        int v = callbackOriginValue[1] + ((int)(y * SCALE_FACTOR));
                        int current = Math.max(0, Math.min(callbackMaxValue[1], v));
                        dig2DepthSeekBar.setProgress(current);
                    }
                }
            };

            callback3 = new CustomGesture.Callback() {
                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    if (digGestureCallback != null){
                        return digGestureCallback.onDoubleTap(e);
                    }
                    return false;
                }

                @Override
                public void onShowPress(MotionEvent e) {
                    if (digGestureCallback != null){
                        digGestureCallback.onShowPress(e);
                    }
                }

                @Override
                public void onDown(MotionEvent e) {
                    callbackOriginValue[0] = dig3LengthSeekBar.getProgress();
                    callbackOriginValue[1] = dig3WidthSeekBar.getProgress();
                    callbackOriginValue[2] = dig3DepthSeekBar.getProgress();
                    callbackMaxValue[0] = dig3LengthSeekBar.getMax();
                    callbackMaxValue[1] = dig3WidthSeekBar.getMax();
                    callbackMaxValue[2] = dig3DepthSeekBar.getMax();
                }

                @Override
                public void onScaleX(float scaleFactor) {
                    if (Status.editedParameter) {
                        dig3LengthSeekBar.setProgress(Math.max(0, Math.min(callbackMaxValue[0],/*计算*/(int) (scaleFactor * callbackOriginValue[0]))));
                    }
                }

                @Override
                public void onScaleY(float scaleFactor) {
                    if (Status.editedParameter) {
                        dig3WidthSeekBar.setProgress(Math.max(0, Math.min(callbackMaxValue[1],/*计算*/(int) (scaleFactor * callbackOriginValue[1]))));
                    }
                }

                @Override
                public void onTranslation(float x, float y) {
                    if (Status.editedParameter){
                        int v = callbackOriginValue[2] + ((int)(y * SCALE_FACTOR));
                        int current = Math.max(0, Math.min(callbackMaxValue[2], v));
                        dig3DepthSeekBar.setProgress(current);
                    }
                }
            };

            bind = true;
        }
    }

    /**
     * 设置参数类型
     * @param context
     * @param count
     */
    public void setParameterCount(Context context,int count){
        switch (count){
            case 2:
                modifiedGesture.setCallback(callback2);
                break;
            case 3:
                modifiedGesture.setCallback(callback3);
                break;
        }
    }

    /**
     * 获取实例
     * @return
     */
    public static DigGestureManager getInstance() {
        if (instance == null){
            instance = new DigGestureManager();
        }
        return instance;
    }

    /**
     * 触摸事件
     * @param event
     * @return
     */
    public boolean onTouchEvent(MotionEvent event){
        if (bind){
            //手势传递下去
            return gestureDetector.onTouchEvent(event);
        }else {
            return false;
        }
    }

    public boolean onDown(MotionEvent event){
        if (bind){
            return modifiedGesture.onDown(event);
        }
        return false;
    }
}
